package com.aloda.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.annotation.AdminAuthCheck;
import com.aloda.annotation.OperateLogAnno;
import com.aloda.constant.ErrorCode;
import com.aloda.dto.app.AppAddDTO;
import com.aloda.dto.app.AppReviewDTO;
import com.aloda.dto.app.AppEditDTO;
import com.aloda.entity.App;
import com.aloda.enums.AppRecommendEnum;
import com.aloda.exception.BaseException;
import com.aloda.exception.WarnException;
import com.aloda.query.AppBrowsePageDTO;
import com.aloda.query.AppPageDTO;
import com.aloda.result.PageResult;
import com.aloda.result.Result;
import com.aloda.service.AppBrowseService;
import com.aloda.service.AppService;
import com.aloda.vo.app.AppBrowseCountVO;
import com.aloda.vo.app.AppInfoVO;
import com.aloda.vo.app.AppPageVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/admin/app")
@RestController("app-admin")
public class AppController {

    @Resource
    private AppService appService;

    @Resource
    private AppBrowseService appBrowseService;

    /**
     * 新增应用
     * @param appAddDTO
     * @return
     */
    @OperateLogAnno(category = "新增应用")
    @PostMapping
    public Result<Long> addApp(@RequestBody AppAddDTO appAddDTO) {
        if (BeanUtil.isEmpty(appAddDTO)) {
            throw new WarnException(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(appService.addApp(appAddDTO));
    }

    /**
     * 分页查询
     * @param appPageDTO
     * @return
     */
    @PostMapping("/page")
    public Result<PageResult<AppPageVO>> pageQuery(@RequestBody AppPageDTO appPageDTO) {
        PageResult<AppPageVO> pageResult = appService.pageQuery(appPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 通过id获取App信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AppInfoVO> getAppById(@PathVariable Long id) {
        AppInfoVO appInfoVO = BeanUtil.copyProperties(appService.getById(id), AppInfoVO.class);
        return Result.success(appInfoVO);
    }


    /**
     * 编辑应用
     * @param appEditDTO
     * @return
     */
    @OperateLogAnno(category = "编辑应用")
    @PutMapping
    public Result<Long> editApp(@RequestBody AppEditDTO appEditDTO) {
        return Result.success(appService.editApp(appEditDTO));
    }

    /**
     * 删除应用
     * @param ids
     * @return
     */
    @AdminAuthCheck
    @OperateLogAnno(category = "删除应用")
    @DeleteMapping
    public Result deleteApp(@RequestBody List<Long> ids) {
        return Result.success(appService.deleteApp(ids));
    }

    /**
     * 导出表格
     * @param response
     */
    @OperateLogAnno(category = "导出应用表格信息")
    @PostMapping("/export")
    public void exportAppToExcel(HttpServletResponse response, @RequestBody List<Long> ids) {
        appService.exportAppToExcel(response, ids);
    }

    /**
     * 审核应用
     * @param appReviewDTO
     * @return
     */
    @OperateLogAnno(category = "审核应用")
    @PostMapping("/review")
    public Result reviewApp(@RequestBody AppReviewDTO appReviewDTO) {
        appService.reviewApp(appReviewDTO);
        return Result.success();
    }

    /**
     * 获取今天所有应用浏览量统计数据
     * @return
     */
    @GetMapping("/browse/today")
    public Result<List<AppBrowseCountVO>> getTodayHotApp () {
        return Result.success(appBrowseService.getTodayHotApp());
    }

    /**
     * 删除指定id的应用今日浏览记录
     * @param id
     * @return
     */
    @OperateLogAnno(category = "删除指定id的应用今日浏览记录")
    @DeleteMapping("/browse/{id}")
    public Result deleteAppBrowse(@PathVariable Long id) {
        return Result.success(appBrowseService.deleteAppBrowseById(id));
    }

    /**
     * 分页查询应用浏览量
     * @param appBrowsePageDTO
     * @return
     */
    @PostMapping("/browse/page")
    public Result<PageResult<AppBrowseCountVO>> pageQueryAppBrowse (@RequestBody AppBrowsePageDTO appBrowsePageDTO) {
        return Result.success(appBrowseService.pageQuery(appBrowsePageDTO));
    }

    /**
     * 导出应用浏览量表格信息
     * @param ids
     * @param response
     */
    @OperateLogAnno(category = "导出应用浏览量表格信息")
    @PostMapping("/browse/export")
    public void exportAppBrowseToExcel(@RequestBody List<Long> ids, HttpServletResponse response) {
        appBrowseService.exportAppBrowseToExcel(ids, response);
    }

    /**
     * 删除应用浏览量数据
     * @param ids
     * @return
     */
    @OperateLogAnno(category = "删除应用浏览量数据")
    @DeleteMapping("/browse")
    public Result deleteAppBrowse(@RequestBody List<Long> ids) {
        return Result.success(appBrowseService.removeByIds(ids));
    }

    /**
     * 设置应用推荐状态
     * @param id 应用id
     * @return
     */
    @OperateLogAnno(category = "设置应用推荐状态")
    @PostMapping("/recommend/{id}/{status}")
    public Result recommendApp(@PathVariable Long id, @PathVariable Integer status) {
        AppRecommendEnum enumByValue = AppRecommendEnum.getEnumByValue(status);
        if (enumByValue == null) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(appService.updateAppRecommendStatus(id, status));
    }
}
